package com.dharma.mydiet.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DAOUser {

	public final static String KEY_ID = "idUser";
	public final static String KEY_NAME = "name";
	public final static String KEY_SURNAME = "surname";
	public final static String KEY_AGE = "age";
	public final static String KEY_HEIGHT = "height";
	public final static String KEY_EMAIL = "email";
	public final static String KEY_PHONE_URGENCIAS = "telefonoUrgencias";
	public final static String KEY_PASSWORD = "password";
	public final static String KEY_ALIAS = "alias";
	public final static String DATABASE_TABLE = "user";
	SQLiteDatabase myDataBase;
	
	private static DAOUser instance;

	private static final String[] cols = new String[] { KEY_ID, KEY_NAME, KEY_SURNAME, KEY_AGE, KEY_HEIGHT, KEY_EMAIL,
			KEY_PHONE_URGENCIAS, KEY_PASSWORD, KEY_ALIAS };

	private DAOUser() {
		myDataBase = DBManager.getInstance().getMyDataBase();
	}

	public static DAOUser getInstance (){
		if (instance==null){
			instance=new DAOUser();
		}
		return instance;
	}
	
	public void insertUser(TransactionalUser user) {
		ContentValues newValues = new ContentValues();
		//newValues.put(KEY_ID, user.getIdUser());
		newValues.put(KEY_NAME, user.getName());
		newValues.put(KEY_SURNAME, user.getSurname());
		newValues.put(KEY_AGE, user.getAge());
		newValues.put(KEY_HEIGHT, user.getHeight());
		newValues.put(KEY_EMAIL, user.getEmail());
		newValues.put(KEY_PHONE_URGENCIAS, user.getTelefonoUrgencias());
		newValues.put(KEY_PASSWORD, user.getPassword());
		newValues.put(KEY_ALIAS, user.getAlias());
		myDataBase.beginTransaction();
		myDataBase.insert(DATABASE_TABLE, null, newValues);
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
	}

	public boolean removeUser(TransactionalUser user) {

		boolean ret = false;
		int row = this.getRow(String.valueOf(user.getIdUser()));
		if (row >= 0) {
			ret = removeUser(row);
		}
		return ret;
	}

	private int getRow(String userID) {
		int row = -1;
		try {
			Cursor result = null;
			result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "='" + userID + "'", null, null, null, null,
					null);

			if (!((result.getCount() == 0) || !result.moveToFirst())) {
				if (result.moveToFirst()) {
					row = result.getInt(result.getColumnIndex(KEY_ID));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	public boolean removeUser(long _rowIndex) {
		myDataBase.beginTransaction();
		boolean result = myDataBase.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
		return result;

	}

	public boolean updateUser(TransactionalUser user) {
		int row = this.getRow(String.valueOf(user.getIdUser()));
		return updateUser(row, user);
	}

	public boolean updateUser(Integer _rowIndex, TransactionalUser user) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_ID, user.getIdUser());
		newValues.put(KEY_NAME, user.getName());
		newValues.put(KEY_SURNAME, user.getSurname());
		newValues.put(KEY_AGE, user.getAge());
		newValues.put(KEY_HEIGHT, user.getHeight());
		newValues.put(KEY_EMAIL, user.getEmail());
		newValues.put(KEY_PHONE_URGENCIAS, user.getTelefonoUrgencias());
		newValues.put(KEY_PASSWORD, user.getPassword());
		newValues.put(KEY_ALIAS, user.getAlias());
		return myDataBase.update(DATABASE_TABLE, newValues, KEY_ID + "=" + _rowIndex, null) > 0;
	}

	public TransactionalUser getUser(String userID) {
		TransactionalUser user = null;
		Cursor result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ALIAS + "= '" + userID + "'", null, null, null,
				null, null);
		if (!((result.getCount() == 0) || !result.moveToFirst())) {
			if (result.moveToFirst()) {
				user = new TransactionalUser(result.getInt(result.getColumnIndex(KEY_ID)), result.getString(result
						.getColumnIndex(KEY_NAME)), result.getString(result.getColumnIndex(KEY_SURNAME)),
						result.getInt(result.getColumnIndex(KEY_AGE)), result.getFloat(result
								.getColumnIndex(KEY_HEIGHT)), result.getString(result.getColumnIndex(KEY_EMAIL)),
						result.getString(result.getColumnIndex(KEY_PHONE_URGENCIAS)), result.getString(result
								.getColumnIndex(KEY_PASSWORD)), result.getString(result.getColumnIndex(KEY_ALIAS)));
			}
		}
		return user;
	}

	public List<TransactionalUser> getUsers() {
		ArrayList<TransactionalUser> devices = new ArrayList<TransactionalUser>();
		Cursor result = myDataBase.query(DATABASE_TABLE, cols, null, null, null, null, KEY_ID);
		if (result.moveToFirst())
			do {
				devices.add(new TransactionalUser(result.getInt(result.getColumnIndex(KEY_ID)), result.getString(result
						.getColumnIndex(KEY_NAME)), result.getString(result.getColumnIndex(KEY_SURNAME)), result
						.getInt(result.getColumnIndex(KEY_AGE)), result.getFloat(result.getColumnIndex(KEY_HEIGHT)),
						result.getString(result.getColumnIndex(KEY_EMAIL)), result.getString(result
								.getColumnIndex(KEY_PHONE_URGENCIAS)), result.getString(result
								.getColumnIndex(KEY_PASSWORD)), result.getString(result.getColumnIndex(KEY_ALIAS))));
			} while (result.moveToNext());
		return devices;
	}

	public static void create(TransactionalUser user) {
	}

	public static TransactionalUser read(int idUser) {
		return null;
	}

	public static void update(TransactionalUser user) {

	}

	public static void delete(int idUser) {

	}
}
